<?php

namespace EM\NewsBundle\Controller;
use EM\NewsBundle\Entity\Users;
use EM\NewsBundle\Form\UserType;
use EM\NewsBundle\Form\GeoLangRelationType;
use \EM\NewsBundle\Entity\GeoLangRelation;
use \EM\NewsBundle\Entity\UserGeoLangSettings;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use EM\NewsBundle\Services\UtilsService;

class UserController extends Controller
{
    

	public function showAction(){
	
		$repository=$this->getDoctrine()->getRepository('EMNewsBundle:Users');
		$users = $repository->findAll();
		return $this->render('EMNewsBundle:User:list.html.twig',array('users' => $users));
	
	}
        public function langSettingsAction($username){
        $em = $this->getDoctrine()->getManager();
        $geolang = new GeoLangRelation();
        $user_gl = new UserGeoLangSettings();
        $form = $this->createForm(new GeoLangRelationType(), $geolang);
      //  $languages=$em->getRepository('EMNewsBundle:Languages')->findAll();
        $geo=$em->getRepository('EMNewsBundle:Geography')->findByLevel(2);
        $lang=$em->getRepository('EMNewsBundle:GeoLangRelation')->findAll();
        $user=$em->getRepository('EMNewsBundle:Users')->findOneBy(array('username' => $username));
        return $this->render('EMNewsBundle:User:userSettingsLang.html.twig'
                                , array(
                                    'form' => $form->createView(),
                                    'user' => $user,
                                    'geo' => $geo
                )); 
        
        
        }
        public function saveUserLangAction(Request $request){
          //  $langs=$this->get('request')->request->get('langs');
            $userid=$this->get('request')->request->get('userid');
            $em = $this->getDoctrine()->getManager();
            
            $geolang=new GeoLangRelation();
            $form = $this->createForm(new GeoLangRelationType(), $geolang);
            $user=$em->getRepository('EMNewsBundle:Users')->find($userid);
            if($request->getMethod() == 'POST')
            {

            $form->bind($request);

            }
          
//            if($langs!=""){
//                        foreach($langs as $lng){
//                                $userLangGeo=new UserGeoLangSettings();
//                                echo $lng;
//                                $user=$em->getRepository('EMNewsBundle:Users')->find($userid);
//                                $userLangGeo->setUser($user);
//                                $lng_s=$em->getRepository('EMNewsBundle:GeoLangRelation')->find($lng);
//                                $userLangGeo->setGeoLang($lng_s);
//                                $em->persist($userLangGeo);
//                                $em->flush();
//                        }
//            }
            
            return "Hola";
        }
        public function getAvLanAction($countryid){
           $em = $this->getDoctrine()->getManager();
           $userRelations=$em->getRepository('EMNewsBundle:UserGeoLangSettings')->findAll();
           $languages=$em->getRepository('EMNewsBundle:GeoLangRelation')->findBy(array('geo' => $countryid));
             return $this->render('EMNewsBundle:User:listLanguagesAjax.html.twig'
                                , array(
                                    'lang' => $languages,
                                    'userRelations' => $userRelations
                                    )); 
        }
        
    
    
	
}
